﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using System.Diagnostics;


namespace EstevesRecordersProject
{
    /// <summary>
    /// Interaction logic for WindowAddNewRecorder.xaml
    /// </summary>
    public partial class WindowAddNewRecorder : Window
    {
        private String message;
        public String Message { get { return message; } set { message = value; lblMessage.Content = value; } }

        private double startWidthF;
        private double startHeightF;
        private double startWidthG;
        private double startHeightG;
        private double canvasHeight;

        Employee employee = null;

        public WindowAddNewRecorder()
        {
            InitializeComponent();
            Message = "";


            startHeightF = this.Height;
            startHeightG = mainGrid.Height;
            startWidthF = this.Width;
            startWidthG = mainGrid.Width;
            canvasHeight = canvasLine.Height;

            tbNrOfLines.Text = "3";

        }

        private void InitializeComboBoxEmployees()
        {
            List<Employee> employees = Repository.GetAllEmployees().OrderBy( e => e.lastName ).ToList();
            comboBoxEmployees.ItemsSource = employees;
            comboBoxEmployees.SelectedValuePath = "ID";
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                InitializeComboBoxEmployees();
            }
            catch (Exception ex)
            {
                Message = ex.Message;
                lblMessage.Foreground = Brushes.Red;
            }
        }

        private void tbNrOfLines_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                Message = "";

                String tbText = tbNrOfLines.Text;
                int nrLines = Int32.Parse(tbText);

                MakeLineDataCanvas();
            }
            catch (Exception ex)
            {
                Message = ex.Message;
                lblMessage.Foreground = Brushes.Red;
            }
        }

        private void MakeLineDataCanvas()
        {
            try
            {
                Message = "";
                canvasLine.Children.Clear();

                String tbText = tbNrOfLines.Text;
                int nrLines = Int32.Parse(tbText);

                for (int i = 0; i < nrLines; i++)
                {
                    Label l = new Label();
                    l.Name = "lblLine" + (i + 1).ToString();
                    l.Content = "Line " + (i + 1).ToString() + " :";
                    l.Margin = new Thickness(lblName.Margin.Left,  10 + i * 30, 0, 0);

                    TextBox tb = new TextBox();
                    tb.Text = "line_" + (i + 1).ToString();
                    tb.Height = 23;
                    tb.Width = 160;
                    tb.Name = "tb" + (i + 1).ToString();
                    tb.Margin = new Thickness(tbName.Margin.Left, 10 + i * 30, 0, 0);
                    

                    canvasLine.Children.Add(l);
                    canvasLine.Children.Add(tb);
                }

                this.canvasLine.Height = (nrLines) * 30;
                
            }
            catch (Exception ex)
            {
                Message = ex.Message;
                lblMessage.Foreground = Brushes.Red;
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Message = String.Empty;
                String name = tbName.Text;
                String name2 = tbTechName.Text;

                if (employee == null) throw new ApplicationException("Select Employee.");

                int idRecorder = Repository.AddNewRecorder(name, name2, employee.number);

                int nr = 0;

                foreach (Control c in canvasLine.Children)
                {
                    if (c is TextBox)
                    {
                        nr++;
                        TextBox tb = c as TextBox;
                        String _name = tb.Text;
                        Repository.AddNewLine(idRecorder, _name, nr);
                    }
                }

                Message = "Recorder " + name  + " successfully added.";
                lblMessage.Foreground = Brushes.Green;

                //TODO: refresh menu
                
                //restart application
                Process.Start(Application.ResourceAssembly.Location);
                Application.Current.Shutdown();

                this.Close();
            }
            catch (Exception ex)
            {
                Message = ex.Message;
                lblMessage.Foreground = Brushes.Red;
            }
        }

        private void comboBoxEmployees_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                String s = comboBoxEmployees.SelectedValue.ToString();
                int idEmployee = Int32.Parse(s);
                employee = Repository.GetEmployeeById(idEmployee);
            }
            catch (Exception ex)
            {
                Message = ex.Message;
                lblMessage.Foreground = Brushes.Red;
            }
        }

    }
}
